import org.junit.jupiter.api.Test;

public class _14_最长公共前缀 {
    @Test
    void test(){
       String []strs = {"flower","flow","flight"};
        System.out.println(longestCommonPrefix(strs));
    }


    public String longestCommonPrefix(String[] strs) {
        if (strs.length==0||strs==null){
            return "";
        }
        //获取字符串中最短的那个字符串的长度
        int minLen=200;
        String res="";
        for (int i = 0; i < strs.length; i++) {
            minLen=strs[i].length()<minLen?strs[i].length():minLen;
        }
        for (int i = 0; i < minLen; i++) {
            //获取第一个字符串的第i个字符
            char c = strs[0].charAt(i);
            boolean flag=true;
            for (int j = 1; j < strs.length; j++) {
                if (strs[j].charAt(i)!=c){
                    flag=false;
                    break;
                }

            }
            if (flag){
                res+=c;
            }else {
                return res;
            }
        }
        return res;
    }
}
